﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Diagnostics;
using LOGISTCBUS.Entities;

namespace LOGISTCBUS.DAO
{
    public class CostDAO : BaseDAO
    {
        #region public methods
        public Boolean insert(Cost c)
        {
            var db = new Db();
            db.Connect();
            String sql = "insert into costs (id_cost_type, id_employee, id_trip, name, value, variable, quantity, description) ";
            sql += "values (@id_cost_type, @id_employee, @id_trip, @name, @value, @variable, @quantity, @description);";
            var parameters = new Dictionary<String, Object>();
            parameters["@id_cost_type"] = c.CostType != null && c.CostType.Id > 0 ? c.CostType.Id : new Nullable<Int32>();
            parameters["@id_employee"] = c.Employee != null && c.Employee.Id > 0 ? c.Employee.Id : new Nullable<Int32>();
            parameters["@id_trip"] = c.Trip != null && c.Trip.Id > 0 ? c.Trip.Id : new Nullable<Int32>();
            parameters["@name"] = c.Name;
            parameters["@value"] = c.Value;
            parameters["@variable"] = c.Variable;
            parameters["@quantity"] = c.Quantity;
            parameters["@description"] = c.Description;
            try
            {
                var sucess = db.Execute(sql, parameters);
                db.Commit();
                db.Disconnect();
                return true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                db.Rollback();
                db.Disconnect();
                return false;
                throw ex;
            }
        }

        public Boolean update(Cost c)
        {
            var db = new Db();
            db.Connect();
            String sql = "update costs set id_cost_type = @id_cost_type, id_employee = @id_employee, id_trip = @id_trip, name = @name, value = @value, variable = @variable, quantity = @quantity, description = @description where id= @id;";
            var parameters = new Dictionary<String, Object>();
            parameters["@id_cost_type"] = c.CostType != null && c.CostType.Id > 0 ? c.CostType.Id : new Nullable<Int32>();
            parameters["@id_employee"] = c.Employee != null && c.Employee.Id > 0 ? c.Employee.Id : new Nullable<Int32>();
            parameters["@id_trip"] = c.Trip != null && c.Trip.Id > 0 ? c.Trip.Id : new Nullable<Int32>();
            parameters["@name"] = c.Name;
            parameters["@value"] = c.Value;
            parameters["@variable"] = c.Variable;
            parameters["@quantity"] = c.Quantity;
            parameters["@description"] = c.Description;
            parameters["@id"] = c.Id;
            try
            {
                var sucess = db.Execute(sql, parameters);
                db.Commit();
                db.Disconnect();
                return true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                db.Rollback();
                db.Disconnect();
                return false;
                throw ex;
            }
        }

        public Cost selectById(Int32 id)
        {
            var db = new Db();
            db.Connect();
            var dt = db.Select("select * from costs where id = " + id);
            db.Disconnect();
            if (dt == null || dt.Rows.Count <= 0)
                return null;
            DataRow r = dt.Rows[0];
            var obj = new Cost()
            {
                Id = Convert.ToInt32(r["id"]),
                CostType = r["id_cost_type"] is DBNull ? new CostType() : new CostType() { Id = Convert.ToInt32(r["id_cost_type"]) },
                Employee = r["id_employee"] is DBNull ? new Employee() : new Employee() { Id = Convert.ToInt32(r["id_employee"]) },
                Trip = r["id_trip"] is DBNull ? new Trip() : new Trip() { Id = Convert.ToInt32(r["id_trip"]) },
                Name = r["name"].ToString(),
                Value = Convert.ToDecimal(r["value"]),
                Variable = r["variable"] is DBNull ? string.Empty : r["variable"].ToString(),
                Quantity = Convert.ToInt32(r["quantity"]),
                Description = r["description"] is DBNull ? string.Empty : r["description"].ToString(),
                Removed = Convert.ToBoolean(r["removed"])
            };
            return obj;
        }

        public List<Cost> select()
        {
            var db = new Db();
            db.Connect();
            var lst = new List<Cost>();
            var dt = db.Select("select * from costs where removed = 0 order by name");
            db.Disconnect();
            foreach (DataRow r in dt.Rows)
            {
                lst.Add(new Cost()
                {
                    Id = Convert.ToInt32(r["id"]),
                    CostType = r["id_cost_type"] is DBNull ? new CostType() : new CostType() { Id = Convert.ToInt32(r["id_cost_type"]) },
                    Employee = r["id_employee"] is DBNull ? new Employee() : new Employee() { Id = Convert.ToInt32(r["id_employee"]) },
                    Trip = r["id_trip"] is DBNull ? new Trip() : new Trip() { Id = Convert.ToInt32(r["id_trip"]) },
                    Name = r["name"].ToString(),
                    Value = Convert.ToDecimal(r["value"]),
                    Variable = r["variable"] is DBNull ? string.Empty : r["variable"].ToString(),
                    Quantity = Convert.ToInt32(r["quantity"]),
                    Description = r["description"] is DBNull ? string.Empty : r["description"].ToString(),
                    Removed = Convert.ToBoolean(r["removed"])
                });
            }
            return lst;
        }

        public List<Cost> select(String name, Int32 page, Int32 pageSize)
        {
            var db = new Db();
            db.Connect();
            var lst = new List<Cost>();
            var sql = "select * from costs where removed = 0";
            if (!String.IsNullOrEmpty(name))
                sql += " and name like '%" + name + "%'";
            sql += " order by name";
            var dt = db.Select(sql);
            db.Disconnect();
            foreach (DataRow r in dt.Rows)
            {
                lst.Add(new Cost()
                {
                    Id = Convert.ToInt32(r["id"]),
                    CostType = r["id_cost_type"] is DBNull ? new CostType() : new CostType() { Id = Convert.ToInt32(r["id_cost_type"]) },
                    Employee = r["id_employee"] is DBNull ? new Employee() : new Employee() { Id = Convert.ToInt32(r["id_employee"]) },
                    Trip = r["id_trip"] is DBNull ? new Trip() : new Trip() { Id = Convert.ToInt32(r["id_trip"]) },
                    Name = r["name"].ToString(),
                    Value = Convert.ToDecimal(r["value"]),
                    Variable = r["variable"] is DBNull ? string.Empty : r["variable"].ToString(),
                    Quantity = Convert.ToInt32(r["quantity"]),
                    Description = r["description"] is DBNull ? string.Empty : r["description"].ToString(),
                    Removed = Convert.ToBoolean(r["removed"])
                });
            }
            return lst.Skip((page - 1) * pageSize).Take(pageSize).ToList();
        }

        public Int32 getCount(String name)
        {
            var count = 0;
            var db = new Db();
            db.Connect();
            var lst = new List<Cost>();
            var sql = "select count(*) as qtd from costs where removed = 0";
            if (!String.IsNullOrEmpty(name))
                sql += " and name like '%" + name + "%'";
            var dt = db.Select(sql);
            db.Disconnect();
            if (dt != null && dt.Rows.Count > 0)
            {
                var r = dt.Rows[0];
                count = Convert.ToInt32(r["qtd"]);
            }
            return count;
        }

        public List<Cost> select(String filter)
        {
            var db = new Db();
            db.Connect();
            var lst = new List<Cost>();
            var dt = db.Select("select * from costs where removed = 0 and " + getConditions(filter, "name") + " order by name");
            db.Disconnect();
            foreach (DataRow r in dt.Rows)
            {
                lst.Add(new Cost()
                {
                    Id = Convert.ToInt32(r["id"]),
                    CostType = r["id_cost_type"] is DBNull ? new CostType() : new CostType() { Id = Convert.ToInt32(r["id_cost_type"]) },
                    Employee = r["id_employee"] is DBNull ? new Employee() : new Employee() { Id = Convert.ToInt32(r["id_employee"]) },
                    Trip = r["id_trip"] is DBNull ? new Trip() : new Trip() { Id = Convert.ToInt32(r["id_trip"]) },
                    Name = r["name"].ToString(),
                    Value = Convert.ToDecimal(r["value"]),
                    Variable = r["variable"] is DBNull ? string.Empty : r["variable"].ToString(),
                    Quantity = Convert.ToInt32(r["quantity"]),
                    Description = r["description"] is DBNull ? string.Empty : r["description"].ToString(),
                    Removed = Convert.ToBoolean(r["removed"])
                });
            }
            return lst;
        }

        public Boolean delete(Cost c)
        {
            var db = new Db();
            db.Connect();
            String sql = getSQLDeleteUpdate("costs", "name");
            var parameters = new Dictionary<String, Object>();
            parameters["@id"] = c.Id;
            var sucess = db.Execute(sql, parameters);
            db.Commit();
            db.Disconnect();
            return sucess;
        }

        public Boolean delete(List<Cost> lst)
        {
            var db = new Db();
            db.Connect();
            var sucess = true;
            foreach (var c in lst)
            {
                String sql = getSQLDeleteUpdate("costs", "name");
                var parameters = new Dictionary<String, Object>();
                parameters["@id"] = c.Id;
                sucess = db.Execute(sql, parameters);
            }
            db.Commit();
            db.Disconnect();
            return sucess;
        }
        #endregion
    }
}
